package com.mlc.api.controller;

import com.mlc.data.entity.Syslogs;
import com.mlc.core.service.SyslogsService;
import com.alibaba.fastjson.JSONObject;
import com.mlc.utils.commom.ResponseResult;
import com.mlc.utils.enums.CodeType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author FireWang
 * @date 2022/03/14 22:43:06
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/syslog")
public class SyslogsController {
    private SyslogsService syslogsService;
    private static JSONObject result;

    /**
     * 获取全部数据列表
     *
     * @return
     */
    @GetMapping("/list")
    public JSONObject getAll() {
        try {
            List<Syslogs> syslogsList = syslogsService.getAll();
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("list", syslogsList);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @GetMapping
    public JSONObject getById(@RequestParam Integer id) {
        try {
            Syslogs syslogs = syslogsService.getById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("data", syslogs);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 新建
     *
     * @param syslogs
     * @return
     */
    @PostMapping
    public JSONObject insert(@RequestBody Syslogs syslogs) {
        try {
            syslogsService.insert(syslogs);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新
     *
     * @param syslogs
     * @return
     */
    @PutMapping
    public JSONObject update(@RequestBody Syslogs syslogs) {
        try {
            syslogsService.update(syslogs);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public JSONObject deleteById(@RequestParam Integer id) {
        try {
            syslogsService.deleteById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }
}